package com.jyw.activity;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jyw.R;
import com.jyw.adapter.QianDaoAdapter;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;
import com.jyw.pojo.SignData;
import com.jyw.thirdparty.DividerGridItemDecoration;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/13
 */

public class QianDaoActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;
    private QianDaoAdapter adapter;
    private TextView tvMonth, tvTian;
    private View vSign;
    private FrameLayout layoutMask;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qiandao);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        tvMonth = (TextView) findViewById(R.id.tv_month);
        tvTian = (TextView) findViewById(R.id.tv_tian);
        vSign = findViewById(R.id.v_sign);
        layoutMask = (FrameLayout) findViewById(R.id.layout_mask);

        vSign.setOnClickListener(this);
        layoutMask.setOnClickListener(this);

        recycler.setLayoutManager(new GridLayoutManager(QianDaoActivity.this, 7));
        recycler.addItemDecoration(new DividerGridItemDecoration(QianDaoActivity.this, 7));
        recycler.setAdapter(adapter = new QianDaoAdapter(QianDaoActivity.this));

        String month = new SimpleDateFormat("yyyy-MM").format(new Date());
        tvMonth.setText(month);

        loadData();
    }

    private void loadData() {
        Request request = new Request.Builder().url(Server.GET_SIGN
                + "?phone_data=" + getPrefs().getString("phone", "")
        ).build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(QianDaoActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONArray arr = new JSONArray(response.body().string());
                    final int num = (int) arr.get(0);
                    JSONArray arr1 = (JSONArray) arr.get(1);
                    Gson gson = new Gson();
                    final List<SignData> list = new ArrayList<>();
                    for (int i = 0; i < arr1.length(); i++) {
                        list.add(gson.fromJson(arr1.get(i).toString(), SignData.class));
                    }
                    new AsyncTask<Void, Void, Void>() {
                        @Override
                        protected Void doInBackground(Void... params) {
                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void aVoid) {
                            super.onPostExecute(aVoid);
                            SpannableString ss = new SpannableString("还差" + (7 - num) + "天可以获得一次抽奖机会~加油哦~！");
                            ss.setSpan(new ForegroundColorSpan(Color.RED), 2, ss.length() - 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tvTian.setText(ss);
                            adapter.swapData(list);
                        }
                    }.execute();
                } catch (JSONException e) {
                    Log.e("simonwLog", "错误：JsonArray创建错误");
                }
            }
        });
    }

    private void signGo() {
        Request request = new Request.Builder().url(Server.ADD_SIGN
                + "?phone=" + getPrefs().getString("phone", "")
                + "&date=" + new SimpleDateFormat("yyyyMMdd").format(new Date())
        ).build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(QianDaoActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                loadData();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.v_sign:
                signGo();
                break;
            case R.id.layout_mask:
                finish();
                break;
        }
    }
}
